package org.flexthinker.binding.bean.support
{
    /**
     * Helps reading a complex property from a path or from an expression.
     * Possible values are:
     * <ul>
     *  <li>propertyName</li>
     *  <li>propertyName.subProperty</li>
     *  <li>propertyName.subProperty1.subProperty2....subPropertyN</li>
     * </ul>
     *
     * @author Dragos Dascalita Haut
     */
    public class PropertyReader
    {
        public function PropertyReader()
        {
        }

        public static function getValue( source:Object, propertyPath:String ):*
        {
            if( !source || !propertyPath || ( propertyPath && propertyPath.length == 0 ) )
            {
                return null;
            }


            var subpaths:Array = propertyPath.split( "." );
            var subpathHost:Object = source;
            try
            {
                for each ( var subpath:String in subpaths )
                {

                    subpathHost = subpathHost[subpath];
                }
            }
            catch ( err:Error )
            {
                throw new PropertyResolutionError( err );
            }

            return subpathHost;
        }

        public static function getElValue( source:Object, el:String ):*
        {
            if( !source || !el || ( el && el.length == 0 ) )
            {
                return null;
            }
            throw new Error("getElValue() method is not implemented yet!");
        }


    }
}